package com.pennywise.service;

import com.pennywise.mapper.ReminderMapper;
import com.pennywise.model.Reminder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 提醒服务
 * 处理所有与提醒相关的业务逻辑，包括定时检查提醒
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ReminderService {
    
    private final ReminderMapper reminderMapper;
    private final NotificationService notificationService;
    private LocalDateTime lastCheckTime = LocalDateTime.now();
    
    /**
     * 添加新的提醒
     *
     * @param reminder 提醒信息
     */
    public void addReminder(Reminder reminder) {
        log.info("添加新提醒: {}", reminder);
        LocalDateTime now = LocalDateTime.now();
        reminder.setCreateTime(now);
        reminder.setUpdateTime(now);
        reminder.setIsActive(true);
        reminderMapper.insert(reminder);
        log.info("提醒添加成功，ID: {}", reminder.getId());
    }
    
    /**
     * 获取提醒详情
     *
     * @param id 提醒ID
     * @return 提醒详情
     */
    public Reminder getReminder(Long id) {
        log.debug("获取提醒，ID: {}", id);
        Reminder reminder = reminderMapper.findById(id);
        if (reminder == null) {
            log.warn("未找到提醒，ID: {}", id);
        }
        return reminder;
    }
    
    /**
     * 更新提醒信息
     *
     * @param reminder 更新的提醒信息
     */
    public void updateReminder(Reminder reminder) {
        log.info("更新提醒: {}", reminder);
        reminder.setUpdateTime(LocalDateTime.now());
        reminderMapper.update(reminder);
        log.info("提醒更新成功，ID: {}", reminder.getId());
    }
    
    /**
     * 删除提醒
     *
     * @param id 要删除的提醒ID
     */
    public void deleteReminder(Long id) {
        log.info("删除提醒，ID: {}", id);
        reminderMapper.deleteById(id);
        log.info("提醒删除成功，ID: {}", id);
    }
    
    /**
     * 定时检查到期提醒
     * 每分钟执行一次
     */
    @Scheduled(fixedRate = 60000)
    public void checkReminders() {
        LocalDateTime currentTime = LocalDateTime.now();
        log.debug("检查到期提醒: {} - {}", lastCheckTime, currentTime);
        
        List<Reminder> dueReminders = reminderMapper.findDueReminders(currentTime, lastCheckTime);
        log.debug("发现{}个到期提醒", dueReminders.size());
        
        for (Reminder reminder : dueReminders) {
            sendReminder(reminder);
        }
        
        lastCheckTime = currentTime;
    }
    
    /**
     * 发送提醒
     * TODO: 实现Windows系统通知
     *
     * @param reminder 要发送的提醒
     */
    private void sendReminder(Reminder reminder) {
        log.info("发送提醒: {}", reminder.getTitle());
        notificationService.sendNotification(reminder.getTitle(), reminder.getContent());
        log.info("提醒发送成功: {}", reminder.getId());
    }

    public void addReminder(String title, String content, LocalDateTime reminderTime) {
        Reminder reminder = new Reminder();
        reminder.setTitle(title);
        reminder.setContent(content);
        reminder.setReminderTime(reminderTime);
        addReminder(reminder);
    }
} 